package com.grasp.forrobchance;


import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.grasp.forrobchance.services.MyAccessibility;
import com.rooten.AppActivity;

import lib.grasp.helper.LinearLayoutHelper;
import lib.grasp.util.SPUtil;
import lib.grasp.util.TOAST;
import lib.grasp.widget.diaglog.EditDialog;

public class MainActivity extends AppActivity implements View.OnClickListener {
    private View mView;
    private LinearLayout mContainer;

    private TextView mTV1;
    private TextView mTV2;
    private TextView mTV3;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //强制竖屏
        super.initView(savedInstanceState);
        mView = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        installView(mView, 1);
        initView();
    }

    @Override
    protected void initActionMenu(MenuItem item) {
        super.initActionMenu(item);
        item.setVisible(false);
        setTitle("普通设置界面");
    }

    /** 初始化布局 */
    private void initView(){
        mContainer = mView.findViewById(R.id.ll_container);

        int value1 = SPUtil.getInt(MyAccessibility.TAG_TIME_LOOP_INTERVAL, 50);
        int value2 = SPUtil.getInt(MyAccessibility.TAG_TIME_TO_DELAY, 750);
        int value3 = SPUtil.getInt(MyAccessibility.TAG_TIME_DELAY_4_PAGE_SWITCH, 10);
        LinearLayoutHelper.addTransportSpace(this, mContainer, 20);
        mTV1 = LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "时间轮询的间隔", String.valueOf(value1), "时间轮询的间隔（毫秒），默认50", "01", false);
        LinearLayoutHelper.addDivider(this, mContainer);
        mTV2 = LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "延迟进活动页面", String.valueOf(value2), "延迟进活动页面（毫秒）,默认750", "02", false);
        LinearLayoutHelper.addDivider(this, mContainer);
        mTV3 = LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "页面切换延时", String.valueOf(value3), "在抢票页面的切换延时（毫秒），默认10", "03", false);
//        LinearLayoutHelper.addTransportSpace(this, mContainer, 20);
//        LinearLayoutHelper.addKeyValueArrowLine(this, this, mContainer, "跳转开发设置界面", "", "", "999", true);
    }

    @Override
    public void onClick(View v) {
        switch ((String)v.getTag()) {

            case "01": {
                int value = SPUtil.getInt(MyAccessibility.TAG_TIME_LOOP_INTERVAL, 50);
                EditDialog.edit(MainActivity.this, "请输入【时间轮询的间隔】，默认50毫秒", String.valueOf(value), new EditDialog.onPositiveClickLister() {
                    @Override
                    public void getText(String text) {
                        if(!checkInt(text)) return;
                        int newValue = Integer.parseInt(text);
                        if(newValue < 10){
                            TOAST.showShort("设置失败，最小10毫秒");
                            return;
                        }
                        SPUtil.putInt(MyAccessibility.TAG_TIME_LOOP_INTERVAL, newValue);
                        mTV1.setText(String.valueOf(newValue));
                    }
                });
                break;
            }
            case "02": {
                int value = SPUtil.getInt(MyAccessibility.TAG_TIME_TO_DELAY, 750);
                EditDialog.edit(MainActivity.this, "请输入【延迟进活动页面】，默认750毫秒", String.valueOf(value), new EditDialog.onPositiveClickLister() {
                    @Override
                    public void getText(String text) {
                        if(!checkInt(text)) return;
                        int newValue = Integer.parseInt(text);
                        if(newValue < 10){
                            TOAST.showShort("设置失败，最小10毫秒");
                            return;
                        }
                        SPUtil.putInt(MyAccessibility.TAG_TIME_TO_DELAY, newValue);
                        mTV2.setText(String.valueOf(newValue));
                    }
                });
                break;
            }
            case "03": {
                int value = SPUtil.getInt(MyAccessibility.TAG_TIME_DELAY_4_PAGE_SWITCH, 10);
                EditDialog.edit(MainActivity.this, "请输入【页面切换延时】，默认10毫秒", String.valueOf(value), new EditDialog.onPositiveClickLister() {
                    @Override
                    public void getText(String text) {
                        if(!checkInt(text)) return;
                        int newValue = Integer.parseInt(text);
                        if(newValue < 5){
                            TOAST.showShort("设置失败，最小5毫秒");
                            return;
                        }
                        SPUtil.putInt(MyAccessibility.TAG_TIME_DELAY_4_PAGE_SWITCH, newValue);
                        mTV3.setText(String.valueOf(newValue));
                    }
                });
                break;
            }
        }
    }

    private boolean checkInt(String str){
        try {
            Integer.parseInt(str);
            return true;
        }
        catch (NumberFormatException e){
            TOAST.showShort("请输入正确的整数");
            return false;
        }
    }}